<?php
namespace Api\Plugin;

use Base\ConstDir\Api\ApiError;
use Zend\Mvc\Controller\Plugin\AbstractPlugin;

abstract class AbstractInviteStrategy extends AbstractPlugin{

    protected $_invitationCode;
    protected $_memberID;
    protected $_inviterInfo;
    private $sm;


    public function __get($val = null){
        if($val == 'sm') {
            $this->sm = $this->getController()->getServiceLocator();
            return $this->sm;
        }
    }

    public function run($options = array()){
        if(empty($options['invitationCode']) || empty($options['memberID'])) throw new \Exception(ApiError::PARAMETER_MISSING_MSG, ApiError::PARAMETER_MISSING);
        $this->_invitationCode = $invitationCode = $options['invitationCode'];
        $this->_memberID = $options['memberID'];

        $this->sm = $this->getController()->getServiceLocator();
        $this->_inviterInfo = $this->sm->get('Api\Model\MemberInfo')->select(array('InvitationCode' => $this->_invitationCode))->current();
        if(empty($this->_inviterInfo) || $this->_inviterInfo['MemberID'] == $this->_memberID) throw new \Exception(ApiError::INVITATION_CODE_IS_NOT_VALID_MSG, ApiError::INVITATION_CODE_IS_NOT_VALID);
        $inviteLog = $this->sm->get('Api\Model\InviteLog')->select(array('InviteeID' => $this->_memberID))->current();
        if(!empty($inviteLog)) throw new \Exception(ApiError::HAVE_BEEN_INVITED_MSG, ApiError::HAVE_BEEN_INVITED);
    }
}